//
//  YueCheMapViewController.m
//  QMCXDriver
//
//  Created by Massimo on 2017/4/13.
//  Copyright © 2017年 Massimo. All rights reserved.
//

#import "YueCheMapViewController.h"
#import <QMMapKit.h>
#import "QCustomAnnotationView.h"
#import "LLSlider.h"
#import "CustomCalloutView.h"
#import "YueCheMapInfoView.h"
#import "QPassenger.h"
@interface YueCheMapViewController ()<QMapViewDelegate,QMSSearchDelegate,LLSliderDelegate>

@property (nonatomic, strong) QMSSearcher *searcher;
@property (nonatomic,weak) IBOutlet QMapView *mapView;
@property (nonatomic,weak) IBOutlet UIButton *navButton;
@property (nonatomic,weak) IBOutlet LLSlider *finishSlider; 
@property (nonatomic,weak) IBOutlet YueCheMapInfoView *mapInfoView;
@property (nonatomic,weak) IBOutlet UIView *finishAlert;
@property (nonatomic,weak) IBOutlet UIView *unFinishiAlert;
@property (nonatomic, assign)BOOL isDriving;




@end

@implementation YueCheMapViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.mapView.delegate = self;
//    self.mapView.showsUserLocation = YES;
    [self.mapView setUserTrackingMode:QMUserTrackingModeNone];
    [self.mapView setZoomLevel:15];
    CLLocationCoordinate2D from =  CLLocationCoordinate2DMake(22.281968, 114.163177);
    [self.mapView setCenterCoordinate:from animated:YES];
    
    
    self.searcher = [[QMSSearcher alloc] init];
    [self.searcher setDelegate:self];
    [self setupPolyLine];
    [self.view sendSubviewToBack:self.mapView];
    NSMutableArray *ps = [[NSMutableArray alloc] init];
    NSArray *names = @[@"脏三",@"李四",@"王武完"];
    NSArray *tels = @[@"132xxxx5555",@"152xxxx5575",@"198xxxx3356"];
    for (int i = 0; i<3; i++) {
        QPassenger *obj = [[QPassenger alloc] init];
        obj.name = names[i];
        obj.phone = tels[i];
        obj.status = arc4random()%2;
        [ps addObject:obj];
    }
    
    self.mapInfoView.passengersArr = ps;
    [self.mapInfoView setPassengers:ps];
    [self.mapInfoView selectPassenger:^(QPassenger *passenger) {
        NSLog(@"%@ -- %d",passenger.name,passenger.status);
    }];
    [self settingsViews];
    self.isDriving = NO;
}

- (void)settingsViews{
    
    
    [self.mapInfoView clickConfirm:^(id station) {
        
        
        __block BOOL isOk = YES;
        [((NSArray*) station ) enumerateObjectsUsingBlock:^(QPassenger  *_Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            if (obj.status == NO) {
                isOk = NO;
                return ;
            }
        }];
        if (isOk) {
            NSLog(@"全员上车  确认上车");
            [self showFinishiAlert];
                        return ;
        }
        [self showUnFInishiAlert];
        NSLog(@"非全员上车  确认上车");
    }];

}
- (void)setIsDriving:(BOOL)isDriving{
    if (_isDriving != isDriving) {
        _isDriving = isDriving;
    }
    self.mapInfoView.hidden = !isDriving;
}

- (void)showFinishiAlert{
    [self.view addSubview: self.finishAlert];
    [self.finishAlert mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.left.right.mas_equalTo(0);
    }];
}

- (void)showUnFInishiAlert{
    [self.view addSubview: self.unFinishiAlert];
    [self.unFinishiAlert mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.left.right.mas_equalTo(0);
    }];
}


- (void)dismissAlert{
    [self.unFinishiAlert removeFromSuperview];
    [self.finishAlert removeFromSuperview];
}


- (IBAction)clickAlertContinue:(id)sender{
    NSLog(@"继续等待");
    [self dismissAlert];
}

- (IBAction)clickAlertGoGoGo:(id)sender{
    
    NSLog(@"立即出发");
    [self.mapInfoView setRouteFinished];
    [self dismissAlert];
}

- (IBAction)clickToDriving:(id)sender{
    self.isDriving = YES;
    [self.view addSubview: self.finishSlider];
    [sender removeFromSuperview];
    __weak typeof(self) weakSelf = self;
    [self.finishSlider mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(weakSelf.view);
        make.bottom.mas_equalTo(-34);
        make.height.mas_equalTo(44);
        make.width.mas_equalTo(230);
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)addAnnotation:(NSString *)title subtitle:(NSString*)subtitle coordinate:(CLLocationCoordinate2D)coordinate{
    QPointAnnotation *annotation = [[QPointAnnotation alloc] init];
    [annotation setTitle:title];
    [annotation setSubtitle:subtitle];
    [annotation setCoordinate:coordinate];
    [annotation setCustomCalloutView:[CustomCalloutView calloutViweWith:annotation.title subtitle:annotation.subTitle]];
    [self.mapView addAnnotations:@[annotation] animated:YES];
}

- (void)setupPolyLine{
    CLLocationCoordinate2D from =  CLLocationCoordinate2DMake(22.281968, 114.163177);
    
    CLLocationCoordinate2D to =  CLLocationCoordinate2DMake(22.270968, 114.161177);
    QMSDrivingRouteSearchOption *driving = [[QMSDrivingRouteSearchOption alloc] init];
    [driving setFromCoordinate:from];
    [driving setToCoordinate:to];
    
    
    CLLocationCoordinate2D mid2 =  CLLocationCoordinate2DMake(22.272968, 114.166177);
    CLLocationCoordinate2D mid1 =  CLLocationCoordinate2DMake(22.279968, 114.163177);
    
    CLLocationCoordinate2D coordinates[2];
    
    coordinates[0] = mid1;
    coordinates[1] = mid2;
    [driving setWayPointsWithCoordinates:coordinates count:2];
    //驾车路线规划支持多种规划策略
    [driving setPolicyWithType:QMSDrivingRoutePolicyTypeLeastDistance];
    [self.searcher searchWithDrivingRouteSearchOption:driving];
    
    
    [self addAnnotation:@"From" subtitle:@"起点" coordinate:from];
    [self addAnnotation:@"mid1" subtitle:@"途径" coordinate:mid1];
    [self addAnnotation:@"mid2" subtitle:@"途径" coordinate:mid2];
    [self addAnnotation:@"to" subtitle:@"终点" coordinate:to];
}
- (void)dealDrivingRoute:(QMSDrivingRouteSearchResult *)drivingRouteResult
{
    QMSRoutePlan *drivingRoutePlan = [[drivingRouteResult routes] firstObject];
    
    NSString *wayPointStr = @"";
    for (int i = 0; i < drivingRoutePlan.waypoints.count; ++i)
    {
        if (i != 0) {
            wayPointStr = [wayPointStr stringByAppendingString:@","];
        }
        QMSRouteWayPoint *wayPoint = [drivingRoutePlan.waypoints objectAtIndex:i];
        wayPointStr = [wayPointStr stringByAppendingString:wayPoint.title];
    }
    
    
    [self.mapView removeOverlays:self.mapView.overlays];
    NSUInteger count = drivingRoutePlan.polyline.count;
    CLLocationCoordinate2D coordinateArray[count];
    for (int i = 0; i < count; ++i)
    {
        [[drivingRoutePlan.polyline objectAtIndex:i] getValue:&coordinateArray[i]];
    }
    
    QPolyline *walkPolyline = [QPolyline polylineWithCoordinates:coordinateArray count:count];
    [self.mapView addOverlay:walkPolyline];
}

#pragma mark - QMSSearchDelegate
- (void)searchWithDrivingRouteSearchOption:(QMSDrivingRouteSearchOption *)drivingRouteSearchOption didRecevieResult:(QMSDrivingRouteSearchResult *)drivingRouteSearchResult{ 
    
    [self dealDrivingRoute:drivingRouteSearchResult];
}

#pragma mark - QMapViewDelegate

- (QOverlayView *)mapView:(QMapView *)mapView viewForOverlay:(id<QOverlay>)overlay
{
    QPolylineView *polylineView = [[QPolylineView alloc] initWithPolyline:overlay];
    
    polylineView.lineWidth = 12/QScreenScale;
    
    polylineView.strokeColor = LLColorRGB(0, 193, 110);
    return polylineView;
}

- (QAnnotationView *)mapView:(QMapView *)mapView viewForAnnotation:(id<QAnnotation>)annotation{
    static NSString *pointAnnotationViewIdentifier = @"customView";
    
    QCustomAnnotationView *annotationView = (QCustomAnnotationView *)[_mapView dequeueReusableAnnotationViewWithIdentifier:pointAnnotationViewIdentifier];
    if (annotationView == nil) {
        annotationView = [[QCustomAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:pointAnnotationViewIdentifier];
        annotationView.imageName = @"icon_map_point";
        annotationView.canShowCallout = YES;
    }
    return annotationView;
}
- (void)mapView:(QMapView *)mapView didSelectAnnotationView:(QAnnotationView *)view{
    
}


#pragma mark - LLSliderDelegate

- (void)slideEnd:(LLSlider *)slider{
    UIAlertAction *action = [UIAlertAction actionWithTitle:@"结束行程" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    UIAlertAction *actionCancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"提示" message:@"是否结束行程" preferredStyle:UIAlertControllerStyleActionSheet];
    
    [alertVC addAction:action];
    [alertVC addAction:actionCancel];
    [self presentViewController:alertVC animated:YES completion:nil];
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
